package com.knowif.system.model;


public class RestBean<T> {
	public final static Integer success_code = 200;
	public final static Integer error_code = 500;
	public final static Integer bad_code = 400;
	public final static Integer NOTUSERID = 401;
	public final static Integer not_code = 404;
	public final static Integer not_authorized  = 401;
	public final static Integer not_userComment  = 402;
	
	
	private long total;
	
	private Integer code = success_code;
	private static String message = "success";
	private T result;
	
	
	public static String SYSTEMERROR = "系统出错，请稍后再试";
	
	
	public RestBean(Integer code) {
		this.code = code;
	}

	public RestBean(MessageEnum m, T result){
		this.result = result;
		if(m != null){
			this.code = m.getCode();
			message = m.getMsg();
		}
	}
	
	public RestBean(Integer code, String message) {
		super();
		this.code = code;
		RestBean.message = message;
	}
	public RestBean(Integer code, String message,T obj,long total) {
		super();
		this.code = code;
		RestBean.message = message;
		this.total = total;
		this.result = obj;
	}
	
	
	public RestBean() {
	}
	
	public RestBean(Integer code, String message, T result) {
		super();
		this.code = code;
		RestBean.message = message;
		this.result = result;
	}

	public RestBean(Integer code, T result) {
		super();
		this.code = code;
		this.result = result;
	}
	
	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static RestBean build(Integer code, String message, Object result) {
		return new RestBean(code,message,result);
	}
	
	@SuppressWarnings({ "rawtypes" })
	public static RestBean build(Integer code) {
		return new RestBean(code);
	}
	
	@SuppressWarnings({ "rawtypes" })
	public static RestBean buildSuccess(String message) {
		return new RestBean(success_code,message);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static RestBean buildSuccess(Object result,long total) {
		return new RestBean(success_code,message,result,total);
	}
	
	@SuppressWarnings({ "rawtypes" })
	public static RestBean build(Integer code, String message) {
		return new RestBean(code,message);
	}
	
	@SuppressWarnings({ "rawtypes" })
	public static RestBean build() {
		return new RestBean();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static RestBean buildSuccess(Object result) {
		return new RestBean(success_code,message,result);
	}
	@SuppressWarnings({ "rawtypes" })
	public static RestBean buildError() {
		return new RestBean(RestBean.bad_code,"system error");
	}
	@SuppressWarnings({ "rawtypes" })
	public static RestBean buildError(String message) {
		return new RestBean(RestBean.bad_code,message);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static RestBean buildError(String message,Object result) {
		return new RestBean(bad_code,message,result);
	}
	
	@SuppressWarnings("rawtypes")
	public static RestBean<?> buildError(String message,Integer code) {
		return new RestBean(code,message);
	}
	
	//判断执行操作是否成功
	public static boolean isOK(Integer code){
		if(success_code == code){
			return true;
		}else{
			return false;
		}
	}
	
	
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		RestBean.message = message;
	}
	public Object getresult() {
		return result;
	}
	public void setresult(T result) {
		this.result = result;
	}
}
